package com.gitee.cui.basic.create;

import lombok.extern.slf4j.Slf4j;

/**
 * 线程的优先级演示
 * @author cuiqingqiang
 * @version 1.0
 * @date 2021/11/10 9:52
 **/
@Slf4j
public class PriorityThread {

    static class PrioritySetThread extends Thread {
        static int threadNo = 1;
        public PrioritySetThread() {
            super("thread-" + threadNo);
            threadNo++;
        }

        private int opportunities = 0;

        @Override
        public void run() {
            while (true) {
                opportunities++;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        PrioritySetThread[] threads = new PrioritySetThread[10];
        for (int i = 0; i < 10; i++) {
            PrioritySetThread thread = new PrioritySetThread();
            thread.setPriority(i + 1);
            threads[i] = thread;
        }

        // 启动线程
        for (int i = 0; i < threads.length; i++) {
            threads[i].start();
        }
        Thread.sleep(1000);
        // 停止线程
        for (int i = 0; i < threads.length; i++) {
            threads[i].stop();
        }
        for (PrioritySetThread thread : threads) {
            log.info(thread.getName() + "-优先级为-" + thread.getPriority() + "-机会值为-" + thread.opportunities);
        }
    }
}
